package com.jieyun.rbac.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jieyun.core.component.page.Page;
import com.jieyun.core.component.page.Pager;
import com.jieyun.core.constant.Constant;
import com.jieyun.core.utils.ServerResponse;
import com.jieyun.rbac.mapper.SysDictMapper;
import com.jieyun.rbac.mapper.SysDictTypeMapper;
import com.jieyun.rbac.po.SysDictType;
import com.jieyun.rbac.service.SysDictTypeService;

/**
 * 数据字典类型服务
 * @author trekxu
 */
@Service
public class SysDictTypeImpl implements SysDictTypeService {
	
	@Resource
	private SysDictMapper SysDictMapper;
	
	@Resource
	private SysDictTypeMapper sysDictTypeMapper;
	/*
	 * (non-Javadoc)
	 * @see com.jieyun.rbac.service.SysDictTypeService#dictTypeList()
	 */
	/**
	 * 其中 type = 1 表示查询可用
	 * 	   type = 0 表示查询不可用
	 *     不传 表示查询全部
	 */
	@Override
	@Page
	public Pager<SysDictType> dictTypePageList(Integer type) {
		if(type == null) {
			return Pager.adapt(this.sysDictTypeMapper.selectList(null));
		}
		
		if(type == 1) {
			return Pager.adapt(this.sysDictTypeMapper.selectList(new EntityWrapper<>(SysDictType.builder().status(Constant.status_ok).build())));
		}
		if(type == 0) {
			return Pager.adapt(this.sysDictTypeMapper.selectList(new EntityWrapper<>(SysDictType.builder().status(Constant.status_fail).build())));
		}
		
		return Pager.noData("传入的参数类型type不正确");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.jieyun.rbac.service.SysDictTypeService#dictTypeAdd(java.lang.String)
	 */
	@Override
	public ServerResponse dictTypeAdd(String name) {
		if(StringUtils.isBlank(name)) {
			return ServerResponse.fail("数据类型参数的名称不能为空");
		}
		
		//判断是否存在相同的字典类型
		int count = this.sysDictTypeMapper.selectCount(new EntityWrapper<SysDictType>(SysDictType.builder().typeName(name).build()));
		if(count != 0) {
			return ServerResponse.fail("已经存在相同名称的字典类型");
		}
		
		SysDictType sysDictType = SysDictType.builder().typeName(name).status(Constant.status_ok).build();
		
		count = this.sysDictTypeMapper.insert(sysDictType);
		if(count == 0) {
			return ServerResponse.fail("添加字典类型失败");
		}
		return ServerResponse.ok("添加字典类型成功");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.jieyun.rbac.service.SysDictTypeService#dictTypeOnOrOff(java.lang.Integer)
	 */
	@Override
	public ServerResponse dictTypeOnOrOff(Integer id) {
		
		if(id ==null) {
			return ServerResponse.fail("被操作的字典类型不能为空");
		}
		
		SysDictType dictType = this.sysDictTypeMapper.selectById(id);
		if(dictType == null) {
			return ServerResponse.fail("字典不存在");
		}
		
		//更新
		int status = Constant.ConvertStatus(dictType.getStatus());
		int count = this.sysDictTypeMapper.updateById(SysDictType.builder().id(id).status(status).build());
		if(count == 0) {
			return ServerResponse.fail("冻结或激活字典类型失败");
		}
		return ServerResponse.ok("冻结或激活字典类型成功");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.jieyun.rbac.service.SysDictTypeService#dictTypeUpdate(java.lang.String)
	 */
	@Override
	public ServerResponse dictTypeUpdate(Integer id,String typeName) {
		if(id ==null) {
			return ServerResponse.fail("被操作的字典类型不能为空");
		}
		
		int count = this.sysDictTypeMapper.selectCount(new EntityWrapper<SysDictType>(SysDictType.builder().typeName(typeName).build()));
		if(count != 0) {
			return ServerResponse.fail("已经存在相同名称的字典类型");
		}
		
		// 现在更新数据字典类型的名称
		SysDictType dictType = SysDictType.builder().id(id).typeName(typeName).build();
		count = this.sysDictTypeMapper.updateById(dictType);
		if(count == 0) {
			return ServerResponse.fail("更新数据字典类型失败");
		}
		return ServerResponse.ok("更新数据字典类型成功");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.jieyun.rbac.service.SysDictTypeService#dictTypeList(java.lang.Integer)
	 */
	@Override
	public List<SysDictType> dictTypeList(Integer type) {
		if(type == null) {
			return this.sysDictTypeMapper.selectList(null);
		}
		
		if(type == 1) {
			return this.sysDictTypeMapper.selectList(new EntityWrapper<>(SysDictType.builder().status(Constant.status_ok).build()));
		}
		if(type == 0) {
			return this.sysDictTypeMapper.selectList(new EntityWrapper<>(SysDictType.builder().status(Constant.status_fail).build()));
		}
		
		return null;
	}

	
	
}
